﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml.Linq;

namespace WEEK14_上课演示
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            Application["online"] = 0;
            Application["total"] = LoadTotalFromFile("c:\\total.txt");
            
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            Application["online"] = Convert.ToInt32(Application["online"]) + 1;
            Application["total"] = Convert.ToInt32(Application["total"]) + 1;

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {
            Application.Lock();
            Application["online"] = Convert.ToInt32(Application["online"]) - 1;
            Application.UnLock();
        }

        protected void Application_End(object sender, EventArgs e)
        {
            SaveTotalToFile("c:\\total.txt", Convert.ToInt32(Application["total"]));
        }

        private void SaveTotalToFile(string filePath, int total)
        {
            StreamWriter writer = new StreamWriter(filePath);
            writer.WriteLine(total);
            writer.Close();
        }

        private int LoadTotalFromFile(string filePath)
        {
            StreamReader reader = new StreamReader(filePath);
            int total = Convert.ToInt32(reader.ReadLine());
            reader.Close();

            return total;
        }
    }
}